<template>
  <div class="change-password">
    <el-form 
      :model="passwordForm" 
      :rules="rules" 
      ref="formRef" 
      label-width="100px"
      class="password-form"
    >
      <el-form-item label="原密码" prop="old_password">
        <el-input 
          v-model="passwordForm.old_password" 
          placeholder="请输入原密码" 
          type="password" 
          show-password 
        />
      </el-form-item>
      <el-form-item label="新密码" prop="new_password">
        <el-input 
          v-model="passwordForm.new_password" 
          placeholder="请输入新密码" 
          type="password" 
          show-password 
        />
      </el-form-item>
      <el-form-item label="确认新密码" prop="confirm_password">
        <el-input 
          v-model="passwordForm.confirm_password" 
          placeholder="请再次输入新密码" 
          type="password" 
          show-password 
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSubmit" :loading="loading">修改密码</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue';
import { ElMessage, ElForm } from 'element-plus';
import { changePassword } from '@/api/user';

// 表单引用
const formRef = ref<InstanceType<typeof ElForm>>();

// 加载状态
const loading = ref(false);

// 密码表单数据
const passwordForm = reactive({
  old_password: '',
  new_password: '',
  confirm_password: ''
});

// 密码匹配校验
const validateConfirmPassword = (rule: any, value: string, callback: any) => {
  if (value === '') {
    callback(new Error('请再次输入新密码'));
  } else if (value !== passwordForm.new_password) {
    callback(new Error('两次输入密码不一致'));
  } else {
    callback();
  }
};

// 表单验证规则
const rules = {
  old_password: [
    { required: true, message: '请输入原密码', trigger: 'blur' },
    { min: 6, max: 30, message: '密码长度应为6-30个字符', trigger: 'blur' }
  ],
  new_password: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    { min: 6, max: 30, message: '密码长度应为6-30个字符', trigger: 'blur' }
  ],
  confirm_password: [
    { required: true, message: '请再次输入新密码', trigger: 'blur' },
    { validator: validateConfirmPassword, trigger: 'blur' }
  ]
};

// 提交表单
const handleSubmit = async () => {
  if (!formRef.value) return;
  
  await formRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true;
      try {
        // 准备提交数据
        const data = {
          old_password: passwordForm.old_password,
          new_password: passwordForm.new_password
        };
        
        // 调用API修改密码
        const res: any = await changePassword(data);
        if (res.code === 200) {
          ElMessage.success('密码修改成功');
          resetForm(); // 重置表单
        } else {
          ElMessage.error(res.message || '修改密码失败');
        }
      } catch (error) {
        console.error('修改密码错误:', error);
        ElMessage.error('修改密码出错');
      } finally {
        loading.value = false;
      }
    }
  });
};

// 重置表单
const resetForm = () => {
  if (formRef.value) {
    formRef.value.resetFields();
  }
};
</script>

<style lang="scss" scoped>
.change-password {
  padding: 20px;
  
  .password-form {
    max-width: 600px;
  }
}
</style>